package com.qcs.sleep.warn;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.qcs.sleep.call.CallTools;
import com.qcs.sleep.warn.action.GroupPushWatcher;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by chshqiang on 13-10-2.
 * 后台运行服务
 * 主要有时间服务  和呼叫服务   提醒服务
 */
public class ICallService extends Service {

    private Timer mTimer;
    private TimerTask mTask;
    private Handler mHandler;

    private BroadcastReceiver broadcastReceiver;

    @Override
    public void onCreate() {
        super.onCreate();

        initializeWatcher();
        initializeTimer();
        initializeBroadcast();
    }

    private void initializeWatcher() {
        CallTools.addWatcher(new GroupPushWatcher());
    }

    private void initializeTimer() {
        mHandler = new TimerHandler(ICallService.this);
        mTimer = new Timer();
        mTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        };

        mTimer.schedule(mTask, 1000l, 1000l);
    }

    private void initializeBroadcast() {
        broadcastReceiver = new SleepBroadcastReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        filter.addAction(Intent.ACTION_SCREEN_ON);

        registerReceiver(broadcastReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null == intent) {
            return START_STICKY_COMPATIBILITY;
        } else if (intent.hasExtra(Constance.KEY_BROADCAST_ACTION)) {
            CallTools.sendAction(intent.getStringExtra(Constance.KEY_BROADCAST_ACTION));
        } else if (intent.hasExtra(Constance.KEY_CALL_PUSH)) {
            CallTools.sendPush(intent.getStringExtra(Constance.KEY_CALL_PUSH));
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new SleepListenerBinder();
    }

    @Override
    public void onDestroy() {
        mTimer.cancel();
        mTask.cancel();
        unregisterReceiver(broadcastReceiver);
        CallTools.removeWatchers();

        super.onDestroy();
    }

    class SleepListenerBinder extends Binder {
    }

    static class TimerHandler extends Handler {
        private WeakReference<Service> serviceWeakReference;

        public TimerHandler(Service service) {
            serviceWeakReference = new WeakReference<Service>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            CallTools.sendTime(serviceWeakReference.get());
        }
    }

    class SleepBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            CallTools.sendAction(intent.getAction());
        }
    }

}
